package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.RuleMenueForm;
import com.woniuxy.controller.info.MenuesIdInfo;
import com.woniuxy.dao.MenusDao;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.MenusNotFountException;
import com.woniuxy.model.Menus;
import com.woniuxy.model.RoleMenus;
import com.woniuxy.dao.RoleMenusDao;
import com.woniuxy.service.RoleMenusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-29
 */
@Service
public class RoleMenusServiceImpl extends ServiceImpl<RoleMenusDao, RoleMenus> implements RoleMenusService {


    @Resource
    private  RoleMenusDao ruleMenusDao;
    @Resource
    private MenusDao menusDao;
    @Override
    public MenuesIdInfo findMenuesId(Integer id) {
        System.out.println(id);
        //根据ruleId差menueId
        QueryWrapper<RoleMenus> wrapper = new QueryWrapper<RoleMenus>().eq("role_id", id);
        List<RoleMenus> ruleMenus = ruleMenusDao.selectList(wrapper);
        List<Integer> ids = new ArrayList<>();
        for(RoleMenus rm :ruleMenus){
            //通过menueid查询menueType,如果type！=0,加入集合
            QueryWrapper<Menus> wrapper1 = new QueryWrapper<Menus>().eq("menus_id", rm.getMenusId());
            Menus menus = menusDao.selectOne(wrapper1);
            if(menus.getMenusType()!=0){
                //添加menueid集合
                ids.add(rm.getMenusId());
            }
        }
        MenuesIdInfo menuesIdInfo = new MenuesIdInfo();
        menuesIdInfo.setMenuesId(ids);
        return menuesIdInfo;
    }

    @Override
    @Transactional(rollbackFor = DdException.class)
    public Boolean updateRuleMenues(RuleMenueForm rmf) throws DdException {
        QueryWrapper<RoleMenus> wrapper = new QueryWrapper<RoleMenus>().eq("role_id", rmf.getRoleId());
        List<RoleMenus> accountRules = ruleMenusDao.selectList(wrapper);
        //如果为null,就直接添加，如果不为null,就先移除在添加
        if(accountRules==null&&accountRules.size()<=0){
            List<Integer> menusIds = rmf.getMenusId();
            if(menusIds.size()<=0){
                ruleMenusDao.delete(wrapper);
            }
            RoleMenus ruleMenus = new RoleMenus();
            ruleMenus.setRoleId(rmf.getRoleId());
//            Integer count = menusDao.selectCount(null);
            for(Integer i:menusIds){
//                if(i>count){
//                    throw new MenusNotFountException(50002,"菜单不存在");
//                }
                ruleMenus.setMenusId(i);
                ruleMenusDao.insert(ruleMenus);
            }
        }else{//如果不为null,先移除
            ruleMenusDao.delete(wrapper);
            //再添加
            List<Integer> menusIds = rmf.getMenusId();
            if(menusIds.size()<=0){
                ruleMenusDao.delete(wrapper);
            }
            RoleMenus ruleMenus = new RoleMenus();
            ruleMenus.setRoleId(rmf.getRoleId());
//            Integer count = menusDao.selectCount(null);
            for(Integer i:menusIds){

//                if(i>count){
//                    throw new MenusNotFountException(50002,"菜单不存在");
//                }
                ruleMenus.setMenusId(i);
                ruleMenusDao.insert(ruleMenus);
            }
        }
        return true;
    }
}
